package com.cloudsiteservice8003.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudsiteservice8003.entity.ServiceInfo;
import com.cloudsiteservice8003.mapper.ServiceInfoMapper;
import com.cloudsiteservice8003.service.ServiceInfoService;
import com.cloudsiteservice8003.util.SnowflakeIdGenerator;
import com.google.common.base.Preconditions;
import org.springframework.stereotype.Service;

/**
 * @author 26407
 * @description 针对表【service_info】的数据库操作Service实现
 * @createDate 2025-06-11 11:32:09
 */
@Service
public class ServiceInfoServiceImpl extends ServiceImpl<ServiceInfoMapper, ServiceInfo>
        implements ServiceInfoService {

    @Override
    public IPage<ServiceInfo> getServiceInfo(Integer pageNum, Integer pageSize, Long siteId, String serviceCode) {
        LambdaQueryWrapper<ServiceInfo> wrapper = new LambdaQueryWrapper<>();
        IPage<ServiceInfo> page = new Page<>(pageNum, pageSize);
        if (siteId != null) {
            wrapper.eq(ServiceInfo::getSiteId, siteId);
            return this.page(page, wrapper);
        }
        if (!StringUtils.isEmpty(serviceCode)) {
            wrapper.eq(ServiceInfo::getServiceCode, serviceCode);
            return this.page(page, wrapper);
        }
        return this.page(page, wrapper);
    }

    @Override
    public Boolean create(ServiceInfo serviceInfo) {
        Preconditions.checkArgument(serviceInfo.getSiteId() != null, "站点ID不能为空");
        SnowflakeIdGenerator snowflakeIdGenerator = new SnowflakeIdGenerator(1,1);
        serviceInfo.setServiceCode(snowflakeIdGenerator.nextId().toString());
        return save(serviceInfo);
    }

    @Override
    public Boolean updates(ServiceInfo serviceInfo) {
        Preconditions.checkArgument(serviceInfo.getId() != null, "id不能为空");
        return updateById(serviceInfo);
    }

    @Override
    public boolean removeByCode(ServiceInfo serviceInfo) {
        if(serviceInfo.getId() != null){
            return removeById(serviceInfo.getId());
        }
        if(serviceInfo.getServiceCode() != null){
            lambdaUpdate().eq(ServiceInfo::getServiceCode, serviceInfo.getServiceCode()).remove();
        }
        if(serviceInfo.getSiteId() != null){
            lambdaUpdate().eq(ServiceInfo::getSiteId, serviceInfo.getSiteId()).remove();
        }
        return false;
    }
}




